<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Tuolaji <479923197@qq.com>
// +----------------------------------------------------------------------
namespace Portal\Controller;

use Common\Controller\AdminbaseController;

class AdminTagController extends AdminbaseController {
	
	protected $tag_model;

	function _initialize() {
		parent::_initialize();
		$this->tag_model = D("Portal/Tag");
	}
	
	// 后台文章分类列表
    public function index(){
		$result = $this->tag_model->order(array("listorder"=>"asc"))->select();
		
		$tree = new \Tree();
		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		foreach ($result as $r) {
			$r['str_manage'] = '<a href="' . U("AdminTag/edit", array("id" => $r['tag_id'])) . '">'.L('EDIT').'</a> | <a class="js-ajax-delete" href="' . U("AdminTag/delete", array("id" => $r['tag_id'])) . '">'.L('DELETE').'</a> ';
			$url=U('portal/list/index',array('id'=>$r['tag_id']));
			$r['url'] = $url;
			$r['id']=$r['tag_id'];
			$array[] = $r;
		}
		
		$tree->init($array);
		$str = "<tr>
					<td><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input input-order'></td>
					<td>\$id</td>
					<td>\$spacer <a href='javascript:void(0);'>\$name</a></td>
					<td>\$str_manage</td>
				</tr>";
		$taxonomys = $tree->get_tree(0, $str);
		$this->assign("taxonomys", $taxonomys);
		$this->display();
	}
	
	// 文章分类添加
	public function add(){
	 	$this->display();
	}
	
	// 文章分类添加提交
	public function add_post(){
		if (IS_POST) {
			if ($this->tag_model->create()!==false) {
				if ($this->tag_model->add()!==false) {
				    F('all_tags',null);
					$this->success("添加成功！",U("AdminTag/index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->tag_model->getError());
			}
		}
	}
	
	// 文章分类编辑
	public function edit(){
		$id = I("get.id",0,'intval');
		$data=$this->tag_model->where(array("tag_id" => $id))->find();
        if($data["taxonomy"]==0){
            $str = '<option value="0" selected>推荐</option><option value="1" >热门</option>';
        }else{
            $str = '<option value="0">推荐</option><option value="1" selected>热门</option>';
        }
		$this->assign("data",$data);
        $this->assign('options', $str);
		$this->display();
	}
	
	// 文章分类编辑提交
	public function edit_post(){
		if (IS_POST) {
			if ($this->tag_model->create()!==false) {
				if ($this->tag_model->save()!==false) {
				    F('all_tags',null);
					$this->success("修改成功！");
				} else {
					$this->error("修改失败！");
				}
			} else {
				$this->error($this->tag_model->getError());
			}
		}
	}
	
	// 文章分类排序
	public function listorders() {
		$status = parent::_listorders($this->tag_model);
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}
	
	// 删除文章分类
	public function delete() {
		$id = I("get.id",0,'intval');

		if ($this->tag_model->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
	
}